/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CastleDefense.units;

import CastleDefense.Animation;
import CastleDefense.Game;
import CastleDefense.Managers.GameManager;
import CastleDefense.units.PlacedUnit;
import CastleDefense.Managers.UnitManager;
import java.awt.Image;
import javax.vecmath.Vector2d;

/**
 *
 * @author Firedemon
 */
public class UnitHunter extends PlacedUnit
{
    private Animation Hunter;
    private boolean   netterClicked;
    private boolean   attachedNet;
    private int       nets;
    private int       lastWave = 0;
    
    public UnitHunter(Vector2d loc, Image img, String name)
    {
        super(loc,img,name);

        Hunter   = new Animation(getAnimation(name), 10);
        lastWave = GameManager.getInstance().getCurrentWave();
        this.setAnimation(Hunter);
        this.resetNets();
    }

     @Override
    public void doLogic()
    {
        if(this.getName().equals("Netter"))
        {
            //if user started a new wave reset the net count
            if(lastWave != GameManager.getInstance().getCurrentWave())
            {
                resetNets();
                lastWave = GameManager.getInstance().getCurrentWave();
            }            
        }
        else if(this.getName().equals("Lancer"))
        {
            if(getTarget() != null && !getTarget().isDead()) //tries to fireEnemy at a target
            {
                targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
                if(targetD > range)
                {
                    setClosestTarget();
                    return;
                }

                if(System.currentTimeMillis() - lastFire < rateOfFire)
                {
                    return;
                }

                lastFire = System.currentTimeMillis();

                Hunter.start();
                if(Hunter.isDone())
                {
                    Hunter.stop();
                    Game.fireAtEnemy(getTarget(), this);
                }
            }
            else if(getTarget() == null || getTarget().isDead())  //sets target if no target has been set
            {
                setClosestTarget();
            }
            else //resets a target if it is too far away
            {
                setClosestTarget();
            }
        }
        else if(this.getName().equals("Gorillas"))
        {
            //TODO
        }
        else if(this.getName().equals("Dragon Slayer"))
        {
            if(getTarget() != null && !getTarget().isDead()) //tries to fireEnemy at a target
            {
                targetD = distance(getLoc().x, getLoc().y, getTarget().getMidX(), getTarget().getMidY());
                if(targetD > range)
                {
                    setClosestTarget();
                    return;
                }

                if(System.currentTimeMillis() - lastFire < rateOfFire)
                {
                    return;
                }

                lastFire = System.currentTimeMillis();

                Hunter.start();
                if(Hunter.isDone())
                {
                    Hunter.stop();
                    Game.fireAtEnemy(getTarget(), this);
                }
            }
            else if(getTarget() == null || getTarget().isDead())  //sets target if no target has been set
            {
                setClosestTarget();
            }
            else //resets a target if it is too far away
            {
                setClosestTarget();
            }
        }
    }
     
    public void setNetterClicked(boolean nc)
    {
        this.netterClicked = nc;
    }
    
    public void setAttachedNet(boolean an)
    {
        this.attachedNet = an;
    }
    
    public boolean isNetAttached()
    {
        return attachedNet;
    }
    
    public void removeNet()
    {
        this.nets--;
        if(nets<0)
            nets=0;
    }
    
    public boolean hasNets()
    {
        return (nets>0);
    }
    
    private void resetNets()
    {
        this.nets = (2 + UnitManager.getInstance().getHunterLevel());
    }
    
    public int getCurrentNets()
    {
        return nets;
    }
}
